Skip to content

Fix ADC conversion start states and add lifecycle diagnostics - #822

Open
gdiciocco wants to merge 3 commits into
FOME-Tech:masterfrom
gdiciocco:fix/adc-lifecycle
Open

gdiciocco wants to merge 3 commits into
FOME-Tech:masterfrom
gdiciocco:fix/adc-lifecycle

Conversation

@gdiciocco

Copy link
Copy Markdown
Contributor

Fixes #586.

The fast ADCv2 timer and software knock paths accept ADC_COMPLETE before calling adcStartConversionI(), but the vendored ChibiOS HAL only permits ADC_READY or ADC_ERROR. A start request during the completion callback can therefore hit the HAL’s not ready assertion, as reported in FOME #586.

Restrict both guards to the permitted states and use adcIsBufferComplete() in the ADCv2/ADCv4 completion callbacks. These changes follow rusEFI PR #7710, also referenced from the upstream panic report rusEFI #7662.

Add adc_stats counters for starts, completed buffers, skipped requests by reason, HAL errors and processed knock buffers. Counter updates use existing serialization; console output copies a snapshot under lock and formats it after unlocking. F4/F7 report individual conversion starts; H7 reports circular-stream starts.

Validation

  • 36 ADCv2 + 21 ADCv4 regressions pass under ASan/UBSan, using production callers and the vendored HAL. Added to CI.
  • 720 general tests pass.
  • Core8 F4, Proteus F7 and Proteus H7 builds pass.
  • Core8 USB/internal-stimulation run with a temporary RAM-only ADC channel assignment: 1,683,687 starts and completions, zero ADC errors and two ADC_ACTIVE skips over approximately 168 seconds. Original firmware and configuration restored and verified afterward.

The changes cover STM32 F4/F7 fast ADC and F4/F7/H7 software knock. Validation includes ADCv2/ADCv4 host tests and firmware builds for all three MCU families. On-device testing was performed on Core8. Physical testing of software knock and F7/H7 boards, plus prolonged load testing, remains pending.

Restrict fast ADCv2 and software knock starts to ADC_READY and ADC_ERROR,
matching the vendored ChibiOS HAL contract. Use adcIsBufferComplete in
ADCv2/ADCv4 callbacks and expose starts, completions, skipped requests and
HAL errors through adc_stats.

Add production-caller/HAL regression tests for ADCv2 and ADCv4 to CI.
Validation: 36 ADCv2 and 21 ADCv4 tests under ASan/UBSan, 720 general tests,
and Core8 F4 / Proteus F7 / Proteus H7 builds. Core8 USB sampling smoke
completed with original firmware and configuration restored afterward.

Refs FOME-Tech#586 and rusefi/rusefi#7710.
Comment thread .github/workflows/build-unit-tests.yaml Outdated
working-directory: ./unit_tests/
run: make -j4 COVERAGE=yes

- name: Run ADC lifecycle tests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this run as part of the next step; Run Tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Updated in fbbc52d: the default build now produces all three test binaries, and Run Tests calls the common make test target, which runs fome_test, ADCv2 and ADCv4 in sequence and propagates any failure. The HAL-specific binaries remain separate. Removed the dedicated ADC CI step.

Validated with a clean coverage build and the common target: 720 engine + 36 ADCv2 + 21 ADCv4 tests pass.

Build all three host test binaries by default and run them through make test
from the existing CI Run Tests step. Keep the ADC-only target available and
update the local test instructions.

Validation: clean coverage build; 720 engine, 36 ADCv2 and 21 ADCv4 tests pass.
Apply the H7 .ram4 section attribute only in production builds and mark
the host fixture explicitly as non-production. Keep the 8192-byte buffer
alignment on both targets. This fixes the ADCv4 build on macOS, where
Mach-O requires segment,section syntax.
@mck1117

mck1117 commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

+1000 is too big. If we want this change, there will need to be some sort of break down.

That said, is this a real issue? I've never seen it happen in real life. I'm not actually sure ADC conversions can fail unless your DMA controller or ADC is broken.

Comment thread firmware/CHANGELOG.md
## Unreleased

### Added
- Add `adc_stats` console diagnostics for fast ADC and software knock: conversion starts, completed buffers, skipped starts by reason, and ADC errors.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not user facing, it doesn't belong here

@gdiciocco gdiciocco Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed

@gdiciocco

Copy link
Copy Markdown
Contributor Author

My concern is a timing window in the HAL’s completion handling: a conversion can succeed, but starting another while the driver is still in ADC_COMPLETE violates adcStartConversionI()’s contract and can trigger a panic. This does not require faulty ADC or DMA hardware. FOME #586 describes the issue, and rusEFI #7710 applies the corresponding guard correction.

I have not established its frequency on current FOME hardware. Even if uncommon, a firmware panic can interrupt engine control, so I think this warrants a proportionate fix. The knock scheduler can preempt ADC completion; the fast ADC guard is defensive with the current IRQ priorities.

I have prepared a simpler revision that removes adc_stats, its console interface, and counters updated during normal acquisition. It retains only minimal anomaly events, reported through existing warnings outside interrupt context.

That said, the revised PR still adds around 970 lines, so I recognise that simplifying the diagnostics does not fully address your concern about its size. Most of those lines support the test harness and regression tests. I would like to retain that coverage in CI to catch future regressions, while separating the infrastructure, guard correction, and warning reporting into independently reviewable commits.

Would that approach address your concerns? I’m happy to push the revised version here if you agree.

@mck1117

mck1117 commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator

I've read through this, which part actually fixes the supposed bug? There isn't an obvious behavior change that I can see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Starting ADC convertion while ADC is in ADC_COMPLETE state causes ChibiOS panic

3 participants